package com.gmt;

import com.gmt.filter.FilterB;
import com.gmt.listener.ListenerB;
import com.gmt.servlet.ServletB;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.Properties;

/**
 * @author John.zhang
 * @version Id: SpringbootApplicationServletApplication, v 1.0 2020/11/11 19:03 ZhiYong Exp $
 * 扫描@WebServlet注解
 */
@SpringBootApplication
@ServletComponentScan
public class SpringbootApplicationServletApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplicationServletApplication.class, args);
    }



    /**
     * 讲servlet封装为ServletRegistrationBean
     * @return
     */
    @Bean
    public ServletRegistrationBean getServletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ServletB());
        //设置映射信息
        servletRegistrationBean.addUrlMappings("/servletB");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new FilterB());
        bean.addUrlPatterns("/filterB");
        return bean;
    }

    @Bean
    public ServletListenerRegistrationBean getListenerRegistrationBean(){
        ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new ListenerB());
        return bean;
    }

    /**
     * 异常信息和对应的 处理地址的 映射
     * @return
     */
    @Bean
    public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){

        SimpleMappingExceptionResolver mapping = new SimpleMappingExceptionResolver();
        Properties mappings = new Properties();
        mappings.setProperty("java.lang.NullPointerException","errorA");
        mappings.setProperty("java.lang.ArithmeticException","errorB");
        mapping.setExceptionMappings(mappings);
        return mapping;
    }
}
